package com.dearbinge.opentapi.content.commodity;


import com.dearbinge.data.api.commodity.CommodityMessageService;
import com.dearbinge.data.dto.commodity.CommodityMessageDto;
import com.dearbinge.data.pojo.commodity.CommodityMessage;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@Api(description = "商品信息")
@RestController
@RequestMapping("/commodity")
@EnableAutoConfiguration
public class CommodityMessageController  extends WebMvcConfigurerAdapter {
    

    @Autowired
    private CommodityMessageService commodityMessageService;

    @ApiOperation(value = "新增（id为空||id为0）/修改",notes = "新增（id为空||id为0）/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public String addOrUpdate(@RequestBody CommodityMessage commodityMessage){
        String dataStr = "系统异常";
        try{
            Integer dataCode = 0;
            if (commodityMessage.getCommodityId() != null && commodityMessage.getCommodityId() != 0){
                dataCode = commodityMessageService.update(commodityMessage);
                if (dataCode > 0){
                    dataStr = "修改成功";
                }
            }else {
                dataCode = commodityMessageService.insert(commodityMessage);
                if (dataCode > 0){
                   dataStr = "添加成功";
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataStr;
    }

    /**
     * 查询全部的商品信息
     */
    @ApiOperation(value = "查询全部",notes = "查询全部")
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public ResultMsg<List<CommodityMessageDto>> getAll(){
        try{
            List<CommodityMessageDto> cmList = commodityMessageService.getAll();
            return ResultMsg.ok(cmList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyWords",value = "关键字",paramType="query"),
            @ApiImplicitParam(name = "isDeleted",value = "是否删除",paramType="query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType="query"),
            @ApiImplicitParam(name = "pageSize",value = "页面记录数",paramType="query")})
    @RequestMapping(value = "/getByAsk",method = RequestMethod.GET)
    public PageResult<CommodityMessageDto> getByAsk(String keyWords, Boolean isDeleted, Integer pageNo, Integer pageSize){
        PageResult<CommodityMessageDto> cmList = new PageResult<>();
        try{
            cmList = commodityMessageService.getByAsk(keyWords,isDeleted,pageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cmList;
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "commodityId",value = "商品ID",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<CommodityMessageDto> getById(Integer commodityId){
        try {
            CommodityMessageDto commodityMessageDto = commodityMessageService.getById(commodityId);
            return ResultMsg.ok(commodityMessageDto);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


}
